//
//  ItemPickerDataSource.m
//  MoneyBook
//
//  Created by Justin Kim on 11. 6. 28..
//  Copyright 2011 Daum. All rights reserved.
//

#import "ItemPickerDataSource.h"
#import "SQLHandler.h"

@implementation ItemPickerDataSource

@synthesize isInArray = _isInArray;
@synthesize itemGroupPickerArray = _itemGroupPickerArray;
@synthesize itemPickerArray = _itemPickerArray;

@synthesize itemGroupName = _itemGroupName;
@synthesize itemGroupId = _itemGroupId;
@synthesize itemName = _itemName;
@synthesize itemId = _itemId;

@synthesize isIn = _isIn;
/////////////////////////////////////////////////////////////////////////////////////////////////
- (id)init{
	self = [super init];
	if (self){
        self.isInArray = [NSArray arrayWithObjects:
                          @"지출",                          
                          @"수입", 
                          nil];
    }
    return self;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc{
    [_isInArray release];
    [_itemGroupPickerArray release];
    [_isInArray release];
    [_itemGroupPickerArray release];
	[_itemPickerArray release];

    [_itemGroupName  release];
    [_itemGroupId release];
    [_itemName release];
    [_itemId release];
	
    [_isIn release];
    [super dealloc];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
-(void)loadItemGroupByIsIn:(NSString*)isIn{
//    NSLog(@"loadItemGroupByIsIn:%@", isIn);
    self.isIn = isIn;
    self.itemGroupPickerArray = [[SQLHandler sharedInstance] findAllItemGroupByIsIn:isIn];
    self.itemGroupId = [[_itemGroupPickerArray objectAtIndex:0] objectForKey:@"item_group_id"];
    self.itemGroupName = [[_itemGroupPickerArray objectAtIndex:0] objectForKey:@"title"];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
-(void)loadItemByItemGroupId:(NSNumber*)itemGroupId{
//    NSLog(@"loadItemByItemGroupId:%d", [itemGroupId intValue]);
    SQLHandler *sql = [SQLHandler sharedInstance];
    NSDictionary *dic =  [sql findItemGroupById:itemGroupId];
    self.itemGroupId = [dic objectForKey:@"item_group_id"];
    self.itemGroupName = [dic objectForKey:@"title"];
    
    self.itemPickerArray = [sql findAllItemByItemGroupById:_itemGroupId];
    if([self.itemPickerArray count]==0){
        self.itemId = [NSNumber numberWithInt:-1];
        self.itemName = @"";
    }else{
        self.itemId = [[_itemPickerArray objectAtIndex:0] objectForKey:@"item_id"];
        self.itemName = [[_itemPickerArray objectAtIndex:0] objectForKey:@"title"];
    }
}

#pragma mark -
#pragma mark UIPickerViewDataSource
/////////////////////////////////////////////////////////////////////////////////////////////////
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
//    NSLog(@"numberOfRowsInComponent:[%d] %d, %d, %d", component, [_isInArray count], [_itemGroupPickerArray count], [_itemPickerArray count]);
    switch (component) {
        case 0:
            return [_isInArray count];
            break;
        case 1:
            return [_itemGroupPickerArray count];
            break;
        case 2:
            return [_itemPickerArray count];
            break;            
        default:
            break;
    }
    return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
//    NSLog(@"numberOfComponentsInPickerView");
    return 3;
}

@end
